﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using TruyenCuoiViet.Source.Helper;
using TruyenCuoiViet.Source.UI;

namespace TruyenCuoiViet.ViewModels
{
    public class CategoryViewModel : INotifyPropertyChanged
    {
        private int _id;
        /// <summary>
        /// Sample ViewModel property; this property is used to identify the object.
        /// </summary>
        /// <returns></returns>
        public int Id
        {
            get
            {
                return _id;
            }
            set
            {
                if (value != _id)
                {
                    _id = value;
                    NotifyPropertyChanged("Id");
                }
            }
        }
        private string _url;
        /// <summary>
        /// Sample ViewModel property; this property is used to identify the object.
        /// </summary>
        /// <returns></returns>
        public string Url
        {
            get
            {
                return _url;
            }
            set
            {
                if (value != _url)
                {
                    _url = value;
                    NotifyPropertyChanged("Url");
                }
            }
        }

        private string _name;
        /// <summary>
        /// Sample ViewModel property; this property is used in the view to display its value using a Binding.
        /// </summary>
        /// <returns></returns>
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                if (value != _name)
                {
                    _name = value;
                    NotifyPropertyChanged("Name");
                }
            }
        }
        private int _page;
        /// <summary>
        /// Sample ViewModel property; this property is used in the view to display its value using a Binding.
        /// </summary>
        /// <returns></returns>
        public int Page
        {
            get
            {
                return _page;
            }
            set
            {
                if (value != _page)
                {
                    _page = value;
                    NotifyPropertyChanged("Page");
                }
            }
        }

        public bool IsDataLoaded = false;
        public ObservableCollection<ItemViewModel> Items { get; private set; }


        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public async void Loaddata()
        {
            if (!IsDataLoaded)
            {
                MainPage.instance.prbLoad.Visibility = Visibility.Visible;
                if(Id!=6)
                Items = await NewsHelper.GetItems(Url, Page);
                else
                    Items = await NewsHelper.GetItems(Url+"?q="+KeyWord, Page);
                NotifyPropertyChanged("Items");
                IsDataLoaded = true;
                MainPage.instance.prbLoad.Visibility = Visibility.Collapsed;
            }
        }
        private string _keyWord;
        /// <summary>
        /// Sample ViewModel property; this property is used in the view to display its value using a Binding.
        /// </summary>
        /// <returns></returns>
        public string KeyWord
        {
            get
            {
                return _keyWord;
            }
            set
            {
                if (value != _keyWord)
                {
                    _keyWord = value;
                    NotifyPropertyChanged("KeyWord");
                }
            }
        }
        public void Next()
        {
            Page++;
            IsDataLoaded = false;
            Loaddata();
        }
        public void GotoPage(int page)
        {
            Page=page;
            IsDataLoaded = false;
            Loaddata();
        }
        public void ReLoadData()
        {
            IsDataLoaded = false;
            Loaddata();
        }

        internal void Prev()
        {
            if (Page > 1)
            {
                Page--;
                IsDataLoaded = false;
                Loaddata();
            }
        }
        public ItemViewModel FindItems(int Id)
        {
            foreach (ItemViewModel n in Items)
            {
                if (n.Id == Id)
                    return n;
            }
            return null;
        }

        internal ItemViewModel FindNextItems(int newsId)
        {
            try
            {
                ItemViewModel nv = FindItems(newsId);
                int k = Items.IndexOf(nv);
                if (k > -1)
                    return Items[k + 1];
                else
                    return null;
            }
            catch
            {
                return null;
            }
        }

        internal ItemViewModel FindPrevItems(int newsId)
        {
            try
            {
                ItemViewModel nv = FindItems(newsId);
                int k = Items.IndexOf(nv);
                if (k > 0)
                    return Items[k - 1];
                else
                    return null;
            }
            catch
            {
                return null;
            }
        }


    }
}